<template>
 <div class="app-container">
   <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="searchForm.goodsSn" clearable class="filter-item" style="width: 200px;" placeholder="请输入商品编号"/>
      <el-input v-model="searchForm.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入商品名称"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>
<!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="tableData" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="宣传画廊">
              <img v-for="pic in props.row.gallery" :key="pic" :src="pic" class="gallery">
            </el-form-item>
            <el-form-item label="商品介绍">
              <span>{{ props.row.brief }}</span>
            </el-form-item>
            <el-form-item label="商品单位">
              <span>{{ props.row.unit }}</span>
            </el-form-item>
            <el-form-item label="关键字">
              <span>{{ props.row.keywords }}</span>
            </el-form-item>
            <el-form-item label="类目ID">
              <span>{{ props.row.categoryId }}</span>
            </el-form-item>
            <el-form-item label="品牌商ID">
              <span>{{ props.row.brandId }}</span>
            </el-form-item>
            <el-form-item label="商品ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column align="center" label="商品编号" prop="goodsSn"/>

      <el-table-column align="center" min-width="100" label="名称" prop="name"/>

      <el-table-column align="center" property="iconUrl" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="40">
        </template>
      </el-table-column>

      <el-table-column align="center" property="iconUrl" label="分享图">
        <template slot-scope="scope">
          <img :src="scope.row.shareUrl" width="40">
        </template>
      </el-table-column>

      <el-table-column align="center" label="详情" prop="detail">
        <template slot-scope="scope">
          <el-dialog :visible.sync="detailDialogVisible" title="商品详情" width="800px">
            <div   v-html="goodsDetail"/>
          </el-dialog>
          <el-button type="primary" size="mini" @click="showDetail(scope.row.detail)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="专柜价格" prop="counterPrice"/>

      <el-table-column align="center" label="当前价格" prop="retailPrice"/>

      <el-table-column align="center" label="是否新品" prop="isNew">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isNew ? 'success' : 'error' ">{{ scope.row.isNew ? '新品' : '非新品' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否热品" prop="isHot">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHot ? 'success' : 'error' ">{{ scope.row.isHot ? '热品' : '非热品' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否在售" prop="isOnSale">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isOnSale ? 'success' : 'error' ">{{ scope.row.isOnSale ? '在售' : '未售' }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
 
  <div class="pagination">
      <el-pagination
      :current-page="page"
      :page-sizes="[10,20,30,40,50,100]"
      :page-size="limit"
      layout="total,sizes, prev, pager, next, jumper"
      :total="dataSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      />
    </div>
     <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>
 </div>
</template>

<script>
import { list,deleteGoods } from '@/api/goods/goods'
import BackToTop from '@/components/BackToTop'
export default {   
   components: { BackToTop },
  data() {
    return {
      searchForm: {
            goodsSn: '',
            name:''
        }, 
        tableData: [{}],
        listLoading: true,
       page: 1,
       limit: 10,
       goodsDetail: '',
        detailDialogVisible: false,
      downloadLoading: false,
       dataSize: 0,
       sort: 'add_time',
       order: 'desc'
    };
  },
   mounted() {
    //获取数据
    this.getDate()
    },
  methods: {
      getDate() {
        this.listLoading = true
        list({ page: this.page,limit: this.limit,
        sort: this.sort,order: this.order,goodsSn: this.searchForm.goodsSn,
        name: this.searchForm.name}).then(response => {
          this.tableData = response.data.data.list,
          this.dataSize = response.data.data.total
          this.listLoading = false
        }).catch(error => {
        
                 this.listLoading = false

        });
      },
       handleUpdate(row) {
      this.$router.push({ path: '/goods/commodityedit', query: { id: row.id }})
    },
      showDetail(detail) {
      this.goodsDetail = detail
      this.detailDialogVisible = true
    },
      //每页条数
    handleSizeChange(val) {
      thus.limit = val
    },
    //第几页
    handleCurrentChange(val) {
      this.page = val
    },
    handleFilter(){
      this.getDate()
    },
    handleCreate() {
      this.$router.push({ path: '/goods/commodityShelves' })
    },
     handleDelete(row) {
        this.$confirm('此操作将永久删除该用户，是否继续?','警告',{
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
                deleteGoods(row).then(response => {
                    this.$notify.success({
                         title: '成功',
                         message: '删除成功'
                    })
                    this.getDate()
                }).catch(error => {
                    this.$notify.error({
                        message: '删除用户失败',
                        type: 'error'
                    })
                })
        }).catch(() => {
            this.$message({
                    message: '已取消删除',
                    type: 'info'
            })
        })
     },
  },
   watch: {
       //监听页数
      page: function() {
          this.getDate()
      },
      //监听页面大小
      limit: function() {
          this.getDate()
      },
    }
};
</script>
<style scoped>

  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .gallery {
    width: 80px;
    margin-right: 10px;
  }
 .filter-container{
        float: right;
        margin-bottom: 10px;
    }
    .filter-item{
        margin-left: 10px;
    }
.pagination{
        margin-top: 10px;
        text-align: left;
    }

</style>